package com.cdxy.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdxy.dmr.dataObject.SceneAttrDO;
import com.cdxy.dmr.dataObject.SceneDO;
import com.cdxy.dmr.dataObject.SceneTicketDO;
import com.cdxy.dmr.repository.SceneAttrRepository;
import com.cdxy.dmr.repository.SceneFacilityRepository;
import com.cdxy.dmr.repository.SceneRepository;
import com.cdxy.dmr.repository.SceneTicketRepository;
import com.cdxy.enums.BizErrorEnum;
import com.cdxy.enums.SaveTypeEnum;
import com.cdxy.model.dto.base.exception.BusinessException;
import com.cdxy.model.dto.base.resp.BasePageResp;
import com.cdxy.model.dto.req.SceneDetailReq;
import com.cdxy.model.dto.req.SearchSceneReq;
import com.cdxy.model.dto.resp.ImgResp;
import com.cdxy.model.dto.resp.SceneDetailResp;
import com.cdxy.model.dto.resp.SearchSceneResp;
import com.cdxy.model.info.SaveInfo;
import com.cdxy.model.info.SceneBookNoticeInfo;
import com.cdxy.model.info.SceneTickInfo;
import com.cdxy.service.IHotSearchService;
import com.cdxy.service.ISceneService;
import com.cdxy.service.IUserSaveService;
import com.cdxy.util.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 景点表 服务实现类
 * </p>
 *
 * @author autoGenCode
 * @since 2020-03-05
 */
@Service
public class SceneServiceImpl implements ISceneService {

    @Autowired
    private SceneRepository sceneRepository;

    @Autowired
    private SceneAttrRepository sceneAttrRepository;

    @Autowired
    private SceneFacilityRepository sceneFacilityRepository;

    @Autowired
    private SceneTicketRepository sceneTicketRepository;

    @Autowired
    private IHotSearchService hotSearchService;

    @Autowired
    private IUserSaveService userSaveService;

    @Override
    public BasePageResp<SearchSceneResp> searchScene(SearchSceneReq searchSceneReq) {
        String city = searchSceneReq.getCity();
        String search = searchSceneReq.getSearchContent();
        Integer axUid = searchSceneReq.getAxUid();
        int size = searchSceneReq.getSize();
        int index = searchSceneReq.getIndex();


        IPage<SceneDO> iPageHotelDO = new Page<>(index, size);
        LambdaQueryWrapper<SceneDO> lambdaQueryWrapper = (new QueryWrapper<SceneDO>()).lambda();
        if (StringUtils.isNotBlank(city) && !city.equals("全国")) {
            lambdaQueryWrapper.eq(SceneDO::getCity, city);
            hotSearchService.searchOnce(city);
        }
        if (StringUtils.isNotBlank(search)) {
            lambdaQueryWrapper.and(obj -> obj.eq(SceneDO::getCity, search).or().eq(SceneDO::getName, search));
            hotSearchService.searchOnce(search);
        }
        iPageHotelDO = sceneRepository.page(iPageHotelDO, lambdaQueryWrapper);
        if (iPageHotelDO.getRecords().size() == 0) {
            iPageHotelDO = new Page<>(0, searchSceneReq.getSize());
            iPageHotelDO = sceneRepository.page(iPageHotelDO, lambdaQueryWrapper);
        }
        List<Integer> searchAddIdList = iPageHotelDO.getRecords().stream().map(SceneDO::getSceneId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(searchAddIdList)) {
            sceneRepository.updateSearchNum(searchAddIdList);
        }

        List<SearchSceneResp> searchHotelRespList = BeanUtil.copyPropertiesByFastJson(iPageHotelDO.getRecords(), SearchSceneResp.class);
        BasePageResp<SearchSceneResp> basePageResp = BeanUtil.copyPropertiesByFastJson(iPageHotelDO, BasePageResp.class);
        basePageResp.setRecords(searchHotelRespList);

        if (axUid != null && searchHotelRespList.size() > 0) {
            List<Integer> ids = searchHotelRespList.stream().map(SearchSceneResp::getSceneId).collect(Collectors.toList());
            List<SaveInfo> saveInfoList = userSaveService.saveStatusList(ids, axUid, SaveTypeEnum.SCENE);
            Map<Integer, SaveInfo> map = saveInfoList.stream().collect(Collectors.toMap(SaveInfo::getBusinessId, obj -> obj));
            searchHotelRespList.forEach(searchHotelResp -> {
                searchHotelResp.setIsSave(map.get(searchHotelResp.getSceneId()).getIsSave());
            });
        }
        basePageResp.setLastPage(size * index >= iPageHotelDO.getTotal());


        return basePageResp;
    }

    @Override
    public SceneDetailResp sceneDetail(SceneDetailReq sceneDetailReq) {
        Integer sceneId = sceneDetailReq.getSceneId();
        Integer axUid = sceneDetailReq.getAxUid();
        SceneDO sceneDO = sceneRepository.getById(sceneId);
        if (sceneDO == null) {
            throw new BusinessException(BizErrorEnum.SCENE_NOT_EXIST);
        }
        SceneDetailResp sceneDetailResp = BeanUtil.copyPropertiesBySpring(sceneDO, SceneDetailResp.class);

        if (axUid != null) {
            SaveInfo saveInfo = userSaveService.saveStatus(axUid, sceneId, SaveTypeEnum.SCENE);
            sceneDetailResp.setIsSave(saveInfo.getIsSave());
        }
        JSONArray noticeArray = JSONArray.parseArray(sceneDO.getBookNotice());
        List<SceneBookNoticeInfo> sceneBookNoticeInfoList = new ArrayList<>(noticeArray.size());
        noticeArray.forEach(notice -> {
            SceneBookNoticeInfo sceneBookNoticeInfo = BeanUtil.copyPropertiesByFastJson(notice, SceneBookNoticeInfo.class);
            sceneBookNoticeInfoList.add(sceneBookNoticeInfo);
        });
        sceneDetailResp.setBookNotice(sceneBookNoticeInfoList);

        List<SceneAttrDO> sceneAttrDOList = sceneAttrRepository.listBySceneId(sceneId);
        List<ImgResp> imgRespList = new ArrayList<>(sceneAttrDOList.size());
        sceneAttrDOList.forEach(sceneAttrDO -> {
            String img = sceneAttrDO.getImg();
            ImgResp imgResp = new ImgResp();
            imgResp.setImgPath(img);
            imgRespList.add(imgResp);
        });
        sceneDetailResp.setImgRespList(imgRespList);

        List<SceneTicketDO> sceneTicketDOList = sceneTicketRepository.listBySceneId(sceneId);
        List<SceneTickInfo> sceneTickInfoList = BeanUtil.copyPropertiesByFastJson(sceneTicketDOList, SceneTickInfo.class);
        sceneDetailResp.setSceneTickInfoList(sceneTickInfoList);

        return sceneDetailResp;
    }
}
